package wlf.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import wlf.pojo.Spu;
import wlf.pojo.Table;
import wlf.service.SpuService;

@Controller
public class SpuController {
	@Resource
	private SpuService spuServiceImpl;
	
	
	@ResponseBody
	@RequestMapping("/allSpu")
	public List<Spu> allSpu(){
		return spuServiceImpl.allSpu();
	}
	
	@RequestMapping("/toSpu")
	public String toSpu(HttpServletRequest request) {
		return "/WEB-INF/backstage/goods/spuManager.jsp";
	}

	@RequestMapping("/toUpdSpu")
	public String toUpdSpu() {
		return "/WEB-INF/backstage/goods/updSpu.jsp";
	}

	@ResponseBody
	@RequestMapping("/spuPage")
	public Table<Spu> spuPage(int page, int limit, Spu spu) {
		System.out.println(spu);
		return spuServiceImpl.page(page, limit, spu);
	}
	
	
	@RequestMapping("/selSpuById")
	public ModelAndView selSpuById(Spu spu,HttpServletRequest request){
		ModelAndView m = new ModelAndView("/WEB-INF/backstage/goods/spu.jsp");
		Table<Spu> table = spuServiceImpl.page(1,1,spu);
		List<Spu> list = table.getData();
		request.setAttribute("spuInfo",list.size()>0&&list!=null?list.get(0):null);
		request.setAttribute("url","updSpu");
		return m;
	}
	
	
	@RequestMapping("/toAddSpu")
	public ModelAndView toAddSpu(HttpServletRequest request){
		ModelAndView m = new ModelAndView("/WEB-INF/backstage/goods/spu.jsp");
		request.setAttribute("url","addSpu");
		return m;
	}
	
	
	@ResponseBody
	@RequestMapping("/addSpu")
	public boolean addSpu(Spu spu){
		int result = spuServiceImpl.addSpu(spu);
		if(result>0){
			return true;
		}
		return false;
	}
	
	@ResponseBody
	@RequestMapping("/delSpu")
	public boolean delSpu(int id){
		int result = spuServiceImpl.delSpu(id);
		if(result>0){
			return true;
		}
		return false;
	}
	@ResponseBody
	@RequestMapping("/updSpu")
	public boolean updSpu(Spu spu){
		int result = spuServiceImpl.updSpu(spu);
		if(result>0){
			return true;
		}
		return false;
	}
	
	@ResponseBody
	@RequestMapping("/updSpuStatus")
	public boolean updSpuStatus(int id,int status){
		int result = spuServiceImpl.updSpuStatus(id,status);
		if(result>0){
			return true;
		}
		return false;
	}
	
	
}
